package event

import (
	"fmt"
	"gitee.com/huanminabc/go-utils/container"
	"github.com/gdamore/tcell/v2"
	"go-TcellTxt/global"
	"go-TcellTxt/utils"
	"os"
)

type TcellEventInfo interface {
	// 事件处理函数
	Handler()
}

var Queue = container.NewQueue[TcellEventInfo]()
var QueueEvent = make(chan bool)

// 注册事件
func RegisterEvent(e TcellEventInfo) {
	Queue.PushTail(e)
	QueueEvent <- true //发送事件触发
}

func init() {
	//执行事件
	go func() {
		for {
			select {
			case <-QueueEvent:
				if Queue.Len() > 0 { //队列长度大于0
					event := Queue.PopHead()
					//执行事件, 不能异步啊,否则会覆盖或者缺失等并发问题
					event.Handler()

				}
			}
		}
	}()
}

func EventRun() {

	s, e := tcell.NewScreen()
	utils.ScreenContentInit()
	if e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}
	if e := s.Init(); e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}
	defer s.Fini()
	global.DefaultStyle = tcell.StyleDefault.
		Background(tcell.ColorBlack).
		Foreground(tcell.ColorWhite)
	s.SetStyle(global.DefaultStyle)
	s.EnableMouse() // 启用鼠标
	s.EnablePaste() // 启用粘贴
	s.EnableFocus() // 启用焦点
	s.Clear()       // 清屏
	s.ShowCursor(0, 0)
	global.Screen = s
	for {

		switch ev := s.PollEvent().(type) {
		case *tcell.EventResize: //	窗口大小改变, 第一次进入也会触发
			s.Sync()                                           // 同步到屏幕
			global.ScreenWidth, global.ScreenHeight = s.Size() //屏幕宽高
			utils.ScreenContentInit()
			utils.InputStrMove(s, global.MouseX, global.MouseY, global.DefaultStyle, ":::")
		case *tcell.EventPaste: //	粘贴事件
		// 这个一般不用处理,自动会将粘贴的内容输入到屏幕光标位置
		case *tcell.EventFocus:
			//焦点事件 ,离开或者进入就会触发 ,一般不用处理
		case *tcell.EventKey: //	键盘事件

			switch ev.Key() {
			case tcell.KeyRune:
				if ev.Rune() == 'i' && ev.Modifiers() == tcell.ModShift { //Shift+i
					//fmt.Println("shift+i")
				} else if ev.Rune() == 's' && ev.Modifiers() == tcell.ModCtrl { //Ctrl+s
					//fmt.Println("ctrl+s")
				} else {
					//发布事件
					RegisterEvent(&InputListen{
						data:  ev.Rune(),
						event: ev,
					})
				}

			case tcell.KeyUp:
				//fmt.Println("up")
			case tcell.KeyDown:
				//fmt.Println("down")
			case tcell.KeyLeft:
				RegisterEvent(&MouseLeftListen{
					data:  ev.Rune(),
					event: ev,
				})
			case tcell.KeyRight:
				//fmt.Println("right")
			case tcell.KeyCtrlA:
				//fmt.Println("ctrl+a")

			case tcell.KeyCtrlV:
				//fmt.Println("ctrl+v")
			case tcell.KeyCtrlD:
				//fmt.Println("ctrl+d")
			case tcell.KeyEnter:
				RegisterEvent(&LFLineFeedListen{})
			case tcell.KeyCtrlL:
				s.Sync()
			case tcell.KeyCtrlC:
			//fmt.Println("ctrl+c")
			case tcell.KeyEscape:
				//fmt.Println("esc")
			case tcell.KeyCtrlQ:
				utils.CloseScreen(s) //关闭屏幕
			default:

			}

		case *tcell.EventMouse: //	鼠标事件
			//x, y := ev.Position()
			switch ev.Buttons() {
			case tcell.ButtonNone: //	鼠标移动

			case tcell.Button1: //	鼠标左键
				//fmt.Fprintf(os.Stderr, "Mouse left button: %d %d\n", ev.Position())
			case tcell.Button2: //	鼠标右键
				//fmt.Fprintf(os.Stderr, "Mouse right button: %d %d\n", ev.Position())
			case tcell.Button3: //	鼠标中键
				//fmt.Fprintf(os.Stderr, "Mouse middle button: %d %d\n", ev.Position())
			case tcell.WheelUp: //	鼠标滚轮向上滚动
				//fmt.Fprintf(os.Stderr, "Mouse wheel up: %d %d\n", ev.Position())
			case tcell.WheelDown: //	鼠标滚轮向下滚动
			//fmt.Fprintf(os.Stderr, "Mouse wheel down: %d %d\n", ev.Position())
			default:

			}

		}

	}
}
